package com.huadao.cart_ccs.experiment.test;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.daolion.utils.log.LoggerUtils;
import com.huadao.cart_ccs.Event.ConsoleEvent;
import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseListFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ConsoleFrag extends BaseListFragment {

    @BindView(R.id.btn_clear)
    Button btn_clear;
    @BindView(R.id.tv_raw)
    TextView tv_raw;
    private List<ConsoleEvent> consolData;
//    private StringBuilder stringBuilder;

    @Override
    public int getContentViewId() {
        return R.layout.frag_console;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public int getRecycleViewId() {
        return R.id.recycleView;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return R.id.swipeRefreshLayout;
    }

    @Override
    protected void init() {
        consolData=new ArrayList<>();
        setEnableLoadMore(false);
        setEnableRefresh(false);
        EventBus.getDefault().register(this);
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                consolData.clear();
                notifyDataSetChanged();
//                stringBuilder.delete(0,stringBuilder.length());
                tv_raw.setText("");
            }
        });
//        stringBuilder=new StringBuilder();
        tv_raw.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_raw.setHorizontallyScrolling(true);
    }

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return null;
    }

    @Override
    protected CommonRecycleAdapter setAdapter() {
        return new ConsoleAdapter(mContext,consolData,R.layout.item_console);
    }

//    @Override
//    protected RecyclerView.Adapter setAdapter() {
//        return new ConsoleAdapter();
//    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(this.getActivity());
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void loadMoreData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ConsoleEvent event) {

        String text = event.getText();
        LoggerUtils.log(getContext(), "发送: " + text);

        if(consolData.size()>100){
            consolData.clear();
            consolData.add(event);
            customeAdapter.notifyDataSetChanged();
        }else {
            consolData.add(event);
            customeAdapter.notifyItemInserted(consolData.size() - 1);
        }
//        notifyItemAInserted(consolData.size()-1,1);
    }

//    @Subscribe(threadMode = ThreadMode.BACKGROUND)
//    public void onMessageEvent(RawConsoleEvent event) {
//        FileUtils.writeRawMessageFile(event.getHexByte());
//    }

    class ConsoleAdapter extends CommonRecycleAdapter<ConsoleEvent>{

        public ConsoleAdapter(Context context, List<ConsoleEvent> dataList, int layoutId) {
            super(context, dataList, layoutId);
        }

        @Override
        protected void bindData(CommonViewHolder holder, ConsoleEvent data) {
            holder.setText(R.id.textView,data.toString());
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {

        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }
    }

//    class ConsoleAdapter extends RecyclerView.Adapter{
//
//        @NonNull
//        @Override
//        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            View view=LayoutInflater.from(getActivity()).inflate(R.layout.item_console,null);
//            return new ConsoleHolder(view);
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//            ConsoleHolder mHolder= (ConsoleHolder) holder;
//            mHolder.textView.setText(consolData.get(position).toString());
//        }
//
//        @Override
//        public int getItemCount() {
//            return consolData.size();
//        }
//    }
//
//    class ConsoleHolder extends RecyclerView.ViewHolder{
//        TextView textView;
//        public ConsoleHolder(View itemView) {
//            super(itemView);
//            textView=itemView.findViewById(R.id.textView);
//        }
//    }
}
